package htmlPicReader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Date;

import org.apache.log4j.Logger;

public class ReadHtmlPic {

	private static Logger log = Logger.getLogger("downtv");
	
	private static final String PICSAVEPATH = "pic/";
	
	private static int CR = 0x0a;
	
	private static int CLCR = 0x0d0a;
	
	private String tvHost = null;
	
	private SocketAddress socketAddress = null;	
	
	public ReadHtmlPic(){		
	}

	public ReadHtmlPic(String tvHost) throws UnknownHostException{
		this.tvHost = tvHost;
		InetAddress address = InetAddress.getByName(this.tvHost);
		this.socketAddress = new InetSocketAddress(address, 80);	
	}
	
	public ReadHtmlPic(String tvHost, SocketAddress socketAddress){
		this.tvHost = tvHost;
		this.socketAddress = socketAddress; 
	}
	
	public void readHtmlPic(String tvPath, String saveFileName) {
		long start = System.currentTimeMillis();
		log.info("Thread " + Thread.currentThread().getName() + " start process: " + tvPath + ", " + saveFileName
				+ ", time: " + new Date());
		
		Socket socket = new Socket();
		File file = null;
		DataInputStream in = null;
		BufferedOutputStream fileOut = null;
		BufferedOutputStream out = null;
		try {
			file = new File(PICSAVEPATH + saveFileName);
			file.createNewFile();
			fileOut = new BufferedOutputStream(new FileOutputStream(file));
			
			socket.connect(socketAddress);
			in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));		
			out = new BufferedOutputStream(socket.getOutputStream());
			String httpGetRequest = new String("GET " + tvPath + " HTTP/1.1"
					+ "\r\n" + "Host: " + tvHost + "\r\n" + "Connection: close" + "\r\n" + "\r\n");
			out.write(httpGetRequest.getBytes());
			out.flush();

			int ch = 0;
			while(ch != -1){
				ch = in.read();
				if(CR == ch){
					ch  = in.readUnsignedShort();
					if(ch == CLCR){
						break;
					}
				}
			}
			
			byte[] buf = null;
			int len = 1024 * 100;
			int readNum = 0;
			int tmpLength = 0;
			while (tmpLength != -1) {
				buf = new byte[len];
				readNum = 0;
				while (readNum < len) {
					tmpLength = in.read(buf, readNum, len - readNum);
					if(tmpLength == -1){
						break;
					}
					fileOut.write(buf, readNum, tmpLength);
					readNum += tmpLength;									
				}
			}
			
		} catch (MalformedURLException e) {
			log.error(e, e);
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e, e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error(e, e);
			e.printStackTrace();
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					log.error(e, e);
					e.printStackTrace();
				}
			}
			fileOut = null;
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					log.error(e, e);
					e.printStackTrace();
				}
			}
			socket = null;
			log.info("Thread " + Thread.currentThread().getName() + " process end:   " + tvPath + ", " + saveFileName
					+ ", time: " + new Date() + ", use time: " + (System.currentTimeMillis() - start));
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String tvHost = "epg.tvsou.com";
		try {			
			ReadHtmlPic readHtml = new ReadHtmlPic(tvHost);			
			String picPath = " /imagesnum/1024086335.gif";
			String saveFN = "1024086335.gif";
			readHtml.readHtmlPic(picPath, saveFN);						
			log.info("End time: " + new Date());
		} catch (Exception e) {
			log.error(e, e);
			e.printStackTrace();
		}		
	}
}